#include "openclosebutton.h"
#include <QEnterEvent>

OpenCloseButton::OpenCloseButton(OpenCloseType type, QWidget* parent)
    : QPushButton(parent),
      m_type(type),
      m_borderColor(Qt::black),  // 边框颜色设为黑色
      m_borderWidth(2),
      m_arrowColor(Qt::black),  // 箭头颜色设为黑色
      m_isHovered(false)
{
    setFixedSize(80, 80);
    setCursor(Qt::PointingHandCursor);
    setAttribute(Qt::WA_Hover);

    // 设置按钮背景为透明
    setStyleSheet("background-color: transparent;");
    // 初始化时生成缓存
    generateCache();
}

void OpenCloseButton::setType(OpenCloseType type)
{
    if (m_type != type)
    {
        m_type = type;
        generateCache();
        update();
    }
}

void OpenCloseButton::setBorderColor(const QColor& color)
{
    if (m_borderColor != color)
    {
        m_borderColor = color;
        generateCache();
        update();
    }
}

void OpenCloseButton::setBorderWidth(int width)
{
    if (m_borderWidth != width && width > 0)
    {
        m_borderWidth = width;
        generateCache();
        update();
    }
}

void OpenCloseButton::setArrowColor(const QColor& color)
{
    if (m_arrowColor != color)
    {
        m_arrowColor = color;
        generateCache();
        update();
    }
}

// void OpenCloseButton::setLineColor(const QColor& color)
//{
//     if (m_lineColor != color)
//     {
//         m_lineColor = color;
//         generateCache();
//         update();
//     }
// }

void OpenCloseButton::generateCache()
{
    m_cache = QPixmap(size());
    m_cache.fill(Qt::transparent);
    QPainter painter(&m_cache);
    painter.setRenderHint(QPainter::Antialiasing, true);

    // 根据当前类型绘制按钮
    int centerX = width() / 2;
    int centerY = height() / 2;

    switch (m_type)
    {
        case OpenCloseType::Open:
            drawOpenCloseButton(painter, centerX, centerY, true);
            break;
        case OpenCloseType::Close:
            drawOpenCloseButton(painter, centerX, centerY, false);
            break;
        case OpenCloseType::Stretch:
            drawStretchCompressButton(painter, centerX, centerY, true);
            break;
        case OpenCloseType::Compress:
            drawStretchCompressButton(painter, centerX, centerY, false);
            break;
    }
}

void OpenCloseButton::drawOpenCloseButton(QPainter& painter, int centerX, int centerY, bool isOpen)
{
    // 绘制中间的竖线
    int lineWidth = qMax(1, static_cast<int>(ceil(this->width() * 0.05)));
    int lineLength = qMax(1, static_cast<int>(ceil(this->height() * 0.75)));
    painter.setPen(QPen(m_arrowColor, lineWidth));
    painter.drawLine(centerX, centerY - lineLength / 2, centerX, centerY + lineLength / 2);

    // 设置箭头颜色
    painter.setPen(Qt::NoPen);
    painter.setBrush(m_arrowColor);

    // 放大箭头大小，确保与边界和分割线保持距离
    int arrowSize = this->width() * 0.25;   // 增大箭头大小
    int arrowOffset = this->width() * 0.1;  // 箭头与分割线的距离
    int baseWidth = this->width() * 0.4;    // 箭头底边宽度

    if (isOpen)
    {
        // 开
        // 左箭头（指向左）- 增加底边宽度
        QPolygonF leftArrow;
        leftArrow << QPointF(centerX - arrowOffset - arrowSize, centerY) << QPointF(centerX - arrowOffset, centerY - baseWidth / 2) << QPointF(centerX - arrowOffset, centerY + baseWidth / 2);
        painter.drawPolygon(leftArrow);

        // 右箭头（指向右）- 增加底边宽度
        QPolygonF rightArrow;
        rightArrow << QPointF(centerX + arrowOffset + arrowSize, centerY) << QPointF(centerX + arrowOffset, centerY - baseWidth / 2) << QPointF(centerX + arrowOffset, centerY + baseWidth / 2);
        painter.drawPolygon(rightArrow);
    }
    else
    {
        // 合
        // 左箭头（指向右）- 增加底边宽度
        QPolygonF leftArrow;
        leftArrow << QPointF(centerX - arrowOffset, centerY) << QPointF(centerX - arrowOffset - arrowSize, centerY - baseWidth / 2) << QPointF(centerX - arrowOffset - arrowSize, centerY + baseWidth / 2);
        painter.drawPolygon(leftArrow);

        // 右箭头（指向左）- 增加底边宽度
        QPolygonF rightArrow;
        rightArrow << QPointF(centerX + arrowOffset, centerY) << QPointF(centerX + arrowOffset + arrowSize, centerY - baseWidth / 2) << QPointF(centerX + arrowOffset + arrowSize, centerY + baseWidth / 2);
        painter.drawPolygon(rightArrow);
    }
}

void OpenCloseButton::drawStretchCompressButton(QPainter& painter, int centerX, int centerY, bool isStretch)
{
    // 绘制中间的横线
    int lineWidth = qMax(1, static_cast<int>(ceil(this->width() * 0.05)));
    int lineLength = qMax(1, static_cast<int>(ceil(this->height() * 0.75)));

    painter.setPen(QPen(m_arrowColor, lineWidth));
    painter.drawLine(centerX - lineLength / 2, centerY, centerX + lineLength / 2, centerY);

    // 设置箭头颜色
    painter.setPen(Qt::NoPen);
    painter.setBrush(m_arrowColor);

    // 放大箭头大小，确保与边界和分割线保持距离
    int arrowSize = this->width() * 0.25;   // 增大箭头大小
    int arrowOffset = this->width() * 0.1;  // 箭头与分割线的距离
    int baseWidth = this->width() * 0.4;    // 箭头底边宽度

    if (isStretch)
    {
        // 拉伸
        // 上箭头（指向上）- 增加底边宽度
        QPolygonF topArrow;
        topArrow << QPointF(centerX, centerY - arrowOffset - arrowSize) << QPointF(centerX - baseWidth / 2, centerY - arrowOffset) << QPointF(centerX + baseWidth / 2, centerY - arrowOffset);
        painter.drawPolygon(topArrow);

        // 下箭头（指向下）- 增加底边宽度
        QPolygonF bottomArrow;
        bottomArrow << QPointF(centerX, centerY + arrowOffset + arrowSize) << QPointF(centerX - baseWidth / 2, centerY + arrowOffset) << QPointF(centerX + baseWidth / 2, centerY + arrowOffset);
        painter.drawPolygon(bottomArrow);
    }
    else
    {
        // 压缩
        // 上箭头（指向下）- 增加底边宽度
        QPolygonF topArrow;
        topArrow << QPointF(centerX, centerY - arrowOffset) << QPointF(centerX - baseWidth / 2, centerY - arrowOffset - arrowSize) << QPointF(centerX + baseWidth / 2, centerY - arrowOffset - arrowSize);
        painter.drawPolygon(topArrow);

        // 下箭头（指向上）- 增加底边宽度
        QPolygonF bottomArrow;
        bottomArrow << QPointF(centerX, centerY + arrowOffset) << QPointF(centerX - baseWidth / 2, centerY + arrowOffset + arrowSize) << QPointF(centerX + baseWidth / 2, centerY + arrowOffset + arrowSize);
        painter.drawPolygon(bottomArrow);
    }
}

void OpenCloseButton::paintEvent(QPaintEvent* event)
{
    Q_UNUSED(event);

    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing, true);

    // 设置按钮背景为透明
    QColor backgroundColor = Qt::transparent;

    // 只有在悬停或按下时才显示背景色
    if (isDown())
    {
        backgroundColor = QColor(200, 200, 200, 120);  // 半透明灰色
    }
    else if (m_isHovered)
    {
        backgroundColor = QColor(220, 220, 220, 50);  // 半透明浅灰色
    }

    // 绘制按钮背景（透明或半透明）
    QRect innerRect = rect().adjusted(m_borderWidth, m_borderWidth, -m_borderWidth, -m_borderWidth);
    painter.setBrush(backgroundColor);
    painter.setPen(Qt::NoPen);
    painter.drawRoundedRect(innerRect, 8, 8);

    // 绘制边框（使用黑色）
    painter.setBrush(Qt::NoBrush);
    painter.setPen(QPen(m_borderColor, m_borderWidth));
    painter.drawRoundedRect(innerRect, 8, 8);

    painter.drawPixmap(0, 0, m_cache);
}

bool OpenCloseButton::event(QEvent* event)
{
    switch (event->type())
    {
        case QEvent::HoverEnter:
            m_isHovered = true;
            update();
            break;
        case QEvent::HoverLeave:
            m_isHovered = false;
            update();
            break;
        default:
            break;
    }
    return QPushButton::event(event);
}

void OpenCloseButton::resizeEvent(QResizeEvent* event)
{
    QPushButton::resizeEvent(event);
    // 当尺寸改变时重新生成缓存
    generateCache();
}